<template>
  <div v-if="visible" :style="{ left: left + 'px', top: top + 'px' }" class="context-menu">
    <div class="menu">
      <!-- 刷新 -->
      <div :class="selectedItems.length > 0 ? 'menu-item' : 'menu-item-readonly'" @click="handleClick('t_xiazai')">
        <i class="el-icon-bottom menu-item-icon" style="color: #0A6ED6;font-weight: 900"></i> 下载
      </div>
      <div :class="selectedItems.length > 0 ? 'menu-item' : 'menu-item-readonly'" @click="handleClick('t_shanchu')">
        <i class="el-icon-close menu-item-icon" style="color: #cc4649;font-weight: 900"></i> 删除
      </div>
    </div>
  </div>
</template>

<script>
import { downLoadZip } from '@/api/zipdownload'
import { delWjglfl, downloadFile } from '@/api/gd/wjglfl'
export default {
  props: {
    selectedItems: {
      type: Array
    }
  },
  data() {
    return {
      visible: false,
      left: 0,
      top: 0,
      node: {},
      option: '',
      cklx: 'table',
      ckList: [{"name":"图标排列","type":"list"},{"name":"列表排列","type":"table"}],
      pxlx: 'mc',
      pxList1: [{"name":"名称","type":"mc"},{"name":"类型","type":"lx"},{"name":"大小","type":"dx"},{"name":"修改时间","type":"xgsj"}],
      pxlx2: 'dz',
      pxList2: [{"name":"递增","type":"dz"},{"name":"递减","type":"dj"}],
    };
  },
  methods: {
    show(event, currentTree) {
      this.visible = true;
      this.left = event.clientX - 40;
      this.top = event.clientY + 10;
      // this.node = currentTree
      // console.log("this.node", this.node)
      console.log("selectedItems", this.selectedItems)
      document.addEventListener('click', this.close);
    },
    close() {
      this.visible = false;
      document.removeEventListener('click', this.close);
    },
    handleClick(action) {
      // 如果当前不是只读，则可以进行修改
      if (this.selectedItems.length > 0) {
        // 下载
        if (action === 't_xiazai') {
          let ids = this.selectedItems.map(item => item.id).toString()
          downLoadZip('/gd/wjglfl/batchDown', "文件_"+this.getFileDownName() , ids.toString())
        }else { // 删除
          let ids = this.selectedItems.map(item => item.id).toString()
          if (ids !== undefined && ids !== "" && ids !== null) {
            this.$modal.confirm('请确认是否要删除文件').then(() => {
              return delWjglfl(ids);
            }).then(() => {
              this.$emit('refresh')
              this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            })
          }
        }
      }
    },
    changeItem(item) {
      this.option = item
    },
    changeCk(item, type) {
      this.$set(this, type, item.type)
    },
    getFileDownName() {
      let date = new Date()
      const year = date.getFullYear();
      const month = ('0' + (date.getMonth() + 1)).slice(-2);
      const day = ('0' + date.getDate()).slice(-2);
      const hours = ('0' + date.getHours()).slice(-2);
      const minutes = ('0' + date.getMinutes()).slice(-2);
      const seconds = ('0' + date.getSeconds()).slice(-2);
      return `${year}${month}${day}${hours}${minutes}${seconds}`;
    },
  },
};
</script>

<style scoped>
.context-menu {
  position: fixed;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  z-index: 1000;
  padding: 5px 2px;
  list-style: none;
}
.menu {
  width: 200px;
  height: 100%;
  padding: 0px 5px;
}
.menu-item {
  width: 100%;
  height: 25px;
  padding-left: 4px;
  line-height: 25px;
  font-size: 12px;
  cursor: pointer;
}
.menu-item:hover {
  background-color: #F2F2F2;
  border-radius: 4px;
}
.menu-item-icon {
  margin-right: 2px;
  font-size: 14px;
  height: 25px;
  line-height: 25px;
}
.menu-item-readonly {
  width: 100%;
  height: 25px;
  padding-left: 4px;
  line-height: 25px;
  font-size: 12px;
  opacity: 0.5;
  cursor: not-allowed;
}
.fgx {
  border: 0.1px solid #EBEEF5;
  margin: 4px 0px;
  border-radius: 2px
}
.context-option {
  margin-left: 190px;
  width: 200px;
  padding: 5px;
}
</style>
